package com.simple.piger.utils;



import com.google.common.collect.Lists;

import java.util.List;

/**
 * 分页工具类
 * @Author  donghj
 * @Date    2018/3/15 19:39
 */
public class PageUtil {
    /**
     * 获取查询起始索引
     * @Author  donghj
     * @Date    2018/3/15 19:39
     */
    public static Integer getPageStart(Integer current ,Integer size) {
        if (null == current) {
            current = 1;
        }
        if (null == size) {
            size = 10;
        }
        return (current - 1) * size;
    }

    /**
     * 对集合数据分页
     * @param result
     * @param pageNum
     * @param pageSize
     * @param <T>
     * @return
     */
    public static <T> List<T> getPageList(List<T> result, int pageNum, int pageSize) {
        if (null == result || result.size() == 0) {
            return Lists.newArrayList();
        }
        int totalRecord = result.size();
        int fromIndex = (pageNum - 1) * pageSize;
        if (fromIndex > totalRecord) {
            fromIndex = (totalRecord / pageSize - 1) * pageSize;
        }
        int record = (pageNum - 1) * pageSize + pageSize;
        result = result.subList(fromIndex, record > totalRecord ? totalRecord : record);
        return result;
    }
}